package com.bjgzy.courselive.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bjgzy.courselive.di.component.DaggerMineOrderComponent;
import com.bjgzy.courselive.mvp.presenter.MineOrderPresenter;
import com.bjgzy.module_couselive.R;
import com.bjgzy.module_couselive.R2;
import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.adapter.HomeFragmentAdapter;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.RightOrderInfo;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.mvp.contract.MineOrderContract;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;

import org.simple.eventbus.Subscriber;

import java.util.List;
import java.util.Locale;

import javax.inject.Inject;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * @author xubo
 * @date 2018-8-3 10:37:55
 */
@Route(path = RouterHub.COURSELIVE_MINEORDERFRAGMENT)
public class MineOrderFragment extends BaseFragment<MineOrderPresenter> implements MineOrderContract.View {
    @BindView(R2.id.rv_list)
    RecyclerView rvList;
    @BindView(R2.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    @Inject
    BaseQuickAdapter<RightOrderInfo, AutoBaseViewHolder> mAdapter;
    @Inject
    List<RightOrderInfo> lists;
    @Inject
    RecyclerView.LayoutManager mManager;
    private HomeFragmentAdapter mFragmentAdapter;
    private int orderStatus = -1;
    private String title;

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerMineOrderComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.courselive_fragment_mine_order, container, false);
    }


    public void setPageAdapter(HomeFragmentAdapter adapter) {
        this.mFragmentAdapter = adapter;
    }

    //更新ViewPager的Title信息
    @Override
    public void updatePagerTitle(int count) {
        if (mFragmentAdapter != null) {
            for (int i = 0; i < mFragmentAdapter.getCount(); i++) {
                if (mFragmentAdapter.getItem(i) == MineOrderFragment.this) {
                    if (title == null) {
                        title = mFragmentAdapter.getPageTitle(i).toString();
                    }
                    mFragmentAdapter.setPageTitle(i, String.format(Locale.CHINESE, "%s  %s", title, count));
                    break;
                }
            }
        }
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        mSwipeLayout.setOnRefreshListener(() -> {
            getDatas(true);
        });
        rvList.setLayoutManager(mManager);
        rvList.setAdapter(mAdapter);
        mAdapter.setOnLoadMoreListener(() -> getDatas(false), rvList);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            RightOrderInfo info = mAdapter.getData().get(position);
            if (info.getOrderStatus() == 0) {
                if (info.getPayPriceStr() == null)
                    info.setPayPriceStr(String.valueOf(info.getItemList().get(0).getDoublePrice()));
                if (info.getTitle() == null) info.setTitle(info.getItemList().get(0).getName());
                Utils.customNavigation(RouterHub.COURSELIVE_ORDERPAYACTIVITY, Constants.ORDERPAY, info);
            }
        });
//        mAdapter.setOnItemLongClickListener((adapter, view, position) -> {
//
//            RightOrderInfo data = mAdapter.getData().get(position);
//            RxUtil.showDeleteDialog(getActivity(), "确定删除该订单吗？", (dialog, index) -> {
//                switch (index) {
//                    case 0:
//                        dialog.dismiss();
//                        break;
//                    case 1:
//                        mAdapter.remove(position);
//                        mAdapter.notifyItemChanged(position);
//                        dialog.dismiss();
//                        break;
//                    default:
//
//                        break;
//                }
//            });
//            return false;
//        });
        getDatas(true);
    }

    private void getDatas(boolean pullToRefresh) {
        mPresenter.getShopOrderInfo(pullToRefresh, orderStatus);
    }

    @Override
    public void setData(@Nullable Object data) {
        this.orderStatus = (int) data;
    }

    @Override
    public void showLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(true);
        }
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(mSwipeLayout)) {
            mSwipeLayout.setRefreshing(false);
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }

    @Subscriber(tag = RouterHub.COURSELIVE_MINEORDERFRAGMENT)
    public void onEvents(Message msg) {
        switch (msg.what) {
            case EventBusTags.PAY_SUCCESS:
                getDatas(true);
                break;
            default:
        }
    }
}
